// ********* DO NOT REMOVE THIS NOTICE **********
// COPYRIGHT�2006 RENEWTEK PTY LTD
// ********* DO NOT REMOVE THIS NOTICE **********

package com.uobgroup.ews.uam.core.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;

import com.uobgroup.ews.uam.core.enums.OperateType;

@Entity
@Configurable
public class ChangeLog {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id")
   private Long id;

   @NotNull
   private OperateType operateType;
   
   @Temporal(TemporalType.TIMESTAMP)
   @NotNull
   private Date operateTime;

   @NotNull
   private String changes;

   @NotNull
   private String tableName;

   @NotNull
   private String userId;

   @NotNull
   private String entityId;

   @Transient
   private Object entity;

   @Transient
   private Integer hashcodeValue = null;

   /**
    * full constructor
    */
   public ChangeLog(Long id, OperateType operateType, Date operateTime, String changes, String tableName, String userId,
         String entityId) {
      this.id = id;
      this.operateType = operateType;
      this.operateTime = operateTime;
      this.changes = changes;
      this.tableName = tableName;
      this.userId = userId;
      this.entityId = entityId;
   }

   /**
    * default constructor
    */
   public ChangeLog() {
   }

   public Long getId() {
      return this.id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public OperateType getOperateType() {
      return this.operateType;
   }

   public void setOperateType(OperateType operateType) {
      this.operateType = operateType;
   }

   public Date getOperateTime() {
      return this.operateTime;
   }

   public void setOperateTime(Date operateTime) {
      this.operateTime = operateTime;
   }

   public String getChanges() {
      return this.changes;
   }

   public void setChanges(String changes) {
      /*int MAX = 2000; // magic number!
      changes = checkMaxLength(changes, MAX);*/
      this.changes = changes;
   }

   private String checkMaxLength(String changes, int MAX) {
      if (changes == null) {
         changes = "";
      }
      if (changes.length() > MAX) {
         changes = changes.substring(0, MAX);
      }
      return changes;
   }

   public String getTableName() {
      return this.tableName;
   }

   public void setTableName(String tableName) {
      this.tableName = tableName;
   }

   public String getUserId() {
      return this.userId;
   }

   public void setUserId(String userId) {
      this.userId = userId;
   }

   public String getEntityId() {
      return this.entityId;
   }

   public void setEntityId(String entityId) {
      int MAX = 20; // magic number!
      entityId = checkMaxLength(entityId, MAX);
      this.entityId = entityId;
   }

   public boolean equals(Object other) {
      if (!(other instanceof ChangeLog)) {
         return false;
      }
      ChangeLog castOther = (ChangeLog) other;
      return new EqualsBuilder().append(this.getId(), castOther.getId()).isEquals();
   }

   public int hashCode() {
      if (hashcodeValue == null) {
         if (id == null) {
            hashcodeValue = super.hashCode();
         }
         else {
            hashcodeValue = generateHashCode();
         }
      }
      return hashcodeValue;
   }

   public int generateHashCode() {
      return new HashCodeBuilder().append(getId()).toHashCode();
   }

   public Object getEntity() {
      return entity;
   }

   public void setEntity(Object entity) {
      this.entity = entity;
   }

   public String toString() {
      return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("id", this.id)
            .append("operateType", this.operateType).append("userId", this.userId)
            .append("operateTime", this.operateTime).append("tableName", this.tableName)
            .append("entityId", this.entityId).append("changes", this.changes).toString();
   }

   public String toStringValue() {
      return new StringBuffer().append("operateType").append(this.operateType).append("userId").append(this.userId)
            .append("operateTime").append(this.operateTime).append("tableName").append(this.tableName)
            .append("entityId").append(this.entityId).append("changes").append(this.changes).toString();
   }
}
